﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace=" System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="TarreOA.TarreOAModels" %>
<%@ Import Namespace="TarreOA.TarreOABLL" %>
<%@ Import Namespace="TarreOA.TarreValidationCode" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.AddTreeViewData(); 
        }  
    }

    protected void AddTreeViewData()
    {
        TreeNode root = new TreeNode();
        root.Text = "全体人员";
        root.Value = "0";
        root.SelectAction = TreeNodeSelectAction.Expand;
        root.ImageUrl = "../../images/Section.png"; 
        SectionServices sectionService = new SectionServices();
        List<Section> firSections = sectionService.findByBaseSectionId(0);
        if (firSections != null && firSections.Count > 0)
        {
            foreach (Section section in firSections)
            {
                TreeNode node = new TreeNode();
                node.Text = section.SectionName;
                node.Value = section.SectionId.ToString();
                node.ImageUrl = "../../images/Sections.png";
                AddTreeViewDataRecrusion(node, section.SectionId);
                if (node.ChildNodes.Count > 0)
                {
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    node.NavigateUrl = "#";
                }
                
                root.ChildNodes.Add(node); 
            }   
        }
        this.TreeView1.Nodes.Add(root); 
    }
    
    /// <summary>
    /// 递归查找部门信息填充树节点
    /// </summary>
    /// <param name="node"></param>
    /// <param name="bsid"></param>
    protected void AddTreeViewDataRecrusion(TreeNode node, int bsid)
    {
        SectionServices sectionServices = new SectionServices();
        List<Section> baseSections = sectionServices.findByBaseSectionId(bsid);
        if (baseSections != null && baseSections.Count > 0)
        {
            foreach (Section section in baseSections)
            {
                TreeNode snode = new TreeNode();
                snode.Text = section.SectionName;
                snode.Value = section.SectionId.ToString();
                snode.ImageUrl = "../../images/Sections.png";
                AddTreeViewDataRecrusion(snode, section.SectionId);
                if (snode.ChildNodes.Count > 0)
                {
                    snode.SelectAction = TreeNodeSelectAction.Expand;
                }
                else
                {
                    snode.NavigateUrl = "#";
                }
                node.ChildNodes.Add(snode);
            }   
        }    
    }

    private string sids = "";
    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        string code  = Code.Codes;
        if ("".Equals(Request.Form.Get(this.txtCatalogName.ClientID)))
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('目录名不许为空！');", true); 
        }
        else if(!code.Equals(Request.Form.Get(this.TextBox1.ClientID).Trim()))
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('验证码错误！');", true); 
        }   
        else
        {
            foreach (TreeNode node in this.TreeView1.Nodes)
            {
                if (node.Checked)
                {
                    sids += (node.Value + ",");
                }
                if (node.ChildNodes.Count > 0)
                {
                    findCheckedNode(node);
                }
            }
            if (FindConfigurationPath.instance().NetDiskRoots[this.txtCatalogName.Text.Trim()] == null)
            {
                FindConfigurationPath.instance().AddNetDiskRoots(this.txtCatalogName.Text.Trim(), this.txtCatalogName.Text.Trim());
            }
            string diskCatalogPath = FindConfigurationPath.instance().NetDiskRoots[this.txtCatalogName.Text.Trim()].ToString();
            NetDisktbServices ndtbService = new NetDisktbServices();
            if (!sids.Equals(""))
            {
                int count = ndtbService.saveDiskCatalogInfo(this.txtCatalogName.Text, diskCatalogPath, sids);
                if (!Directory.Exists(diskCatalogPath))
                {
                    Directory.CreateDirectory(diskCatalogPath);
                }
                if (count == 1)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('目录创建成功！');", true);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('目录创建失败，请重试！');", true);
                }
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('请选择目录开放范围！');", true);
            }   
        }   
    }
    
    /// <summary>
    /// 递归查找选中的树节点
    /// </summary>
    /// <param name="node"></param>
    /// <param name="list"></param>
    protected void findCheckedNode(TreeNode node)
    {
        foreach (TreeNode snode in node.ChildNodes)
        {
            if (snode.Checked)
            {
                sids += (snode.Value + ",");
            }
            if (snode.ChildNodes.Count > 0)
            {
                findCheckedNode(snode); 
            }  
        }   
    } 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>

    <link rel="Stylesheet" type="text/css" href="../../Styles/right_style.css" />
    <script type="text/javascript" src="../../Scripts/jquery.ui/js/jquery.js"></script>
	
    <style type="text/css">
        <!--
        html{ scrollbar-base-color: #dbecf8; scrollbar-arrow-color: #2a8ed1; scrollbar-track-color: #bdddf2; scrollbar-3dlight-color: #2886c5; scrollbar-darkshadow-color: #2886c5;
        scrollbar-face-color: #dbecf8; scrollbar-shadow-color: #dbecf8;  }
        -->
    </style>	
    
</head>
<body>
    <form id="form1" runat="server">
            <div class="sub_right_top">
  <div class="hdm_header" >
    <div class="tb1_on"><h2 class="h2_icon">网络硬盘设置</h2></div>

    <div class="clear"></div>
  </div>
  <div class="hdm_footer">
    <div style="height:20px;"></div>    
    <form id="form2" name="form1" method="post" action="">

    <table width="96%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#77acd0" style="border-collapse:collapse; ">
      <tr bgcolor="#e4f1fd">
        <td colspan="2" align="center">创建共享目录</td>

      </tr>
      <tr>
        <td width="120">共享目录标题：</td>
        <td align="left">
         <asp:TextBox ID="txtCatalogName" runat="server" Width="176px" Height="16px"></asp:TextBox>
        </td>
      </tr>
      <tr>

        <td width="120">验证码：</td>
        <td align="left">
          <asp:TextBox ID="TextBox1" runat="server" Width="176px" Height="16px"></asp:TextBox>
                    <asp:Image ID="Image1" runat="server" Height="22px" ImageAlign="AbsMiddle" ImageUrl="../../ValidationCode.aspx"
                        Width="48px" />
           </td>
      </tr>
      <tr>
        <td width="120" valign="top">开放范围：</td>
        <td>
            <asp:Panel ID="Panel1" runat="server" Height="300px" ScrollBars="Vertical" 
                Width="100%">
                        <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" 
                            ExpandDepth="1" ImageSet="Arrows">
                        </asp:TreeView>
                    </asp:Panel>
         </td>
      </tr>
      <tr>
        <td width="120">&nbsp;</td>

        <td>
            <asp:Button ID="BtnCreate" runat="server" OnClick="BtnCreate_Click" Text=""  CssClass="shared_create"/>
            &nbsp;
            <asp:Button ID="backBtn" runat="server" PostBackUrl="~/admin/NetDiskSetting/NetDiskIndex.aspx" CssClass="fanhui_anniu" />
        </td>
      </tr>
    </table>
   </form> 
   <div style="height:20px;"></div>  
   </div>
</div>

    </form>
</body>
</html>
